package com.xunlai.infra.permission.business.controller;

import com.xunlai.infra.permission.business.service.PosterService;
import com.xunlai.infra.permission.business.vo.poster.*;
import com.xunlai.infra.permission.common.exception.ExceptionCodeEnum;
import com.xunlai.infra.permission.common.security.TenantUser;
import com.xunlai.infra.permission.common.vo.BasePageRespVo;
import com.xunlai.infra.permission.common.vo.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liang
 * @date 8/7/23 9:56 PM
 */
@RestController
@RequestMapping("travel/manage/poster/template")
public class PosterTemplateManageController {

    private PosterService posterService;

    @PostMapping("create")
    public BaseResponse<Long> createPosterTemplate(@RequestBody PosterTemplateCreateVo posterTemplateCreateVo) {
        return BaseResponse.success(posterService.createPosterTemplate(posterTemplateCreateVo));
    }

    @RequestMapping(value = "update/{id}", method = {RequestMethod.POST, RequestMethod.PUT})
    public BaseResponse<Long> updatePosterTemplate(@PathVariable Long id, @RequestBody PosterTemplateCreateVo posterTemplateCreateVo) {
        return BaseResponse.success(posterService.createPosterTemplate(posterTemplateCreateVo));
    }

    @GetMapping("list")
    public BaseResponse<BasePageRespVo<PosterTemplateListVo>> listPosterTemplate(@RequestParam(required = false) Long posterType,
                                                                                 @RequestParam(required = false) String posterName,
                                                                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return BaseResponse.success(posterService.listPosterTemplate(null, posterType,posterName, null, pageNum, pageSize));
    }

    @GetMapping("favorite/list")
    public BaseResponse<BasePageRespVo<PosterTemplateListVo>> listFavoritePosterTemplate(@RequestParam(required = false) Long posterType,
                                                                                         @RequestParam(required = false) String posterName,
                                                                                         @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                                         @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof TenantUser tenantUser){
            String id = tenantUser.getId();
            return BaseResponse.success(posterService.listPosterTemplate(Long.valueOf(id),posterType, posterName, true, pageNum, pageSize));
        }else {
            return BaseResponse.error(ExceptionCodeEnum.NOT_LOGIN);
        }
    }

    @GetMapping("notFavorite/list")
    public BaseResponse<BasePageRespVo<PosterTemplateListVo>> listUnFavoritePosterTemplate(@RequestParam(required = false) Long posterType,
                                                                                           @RequestParam(required = false) String posterName,
                                                                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof TenantUser tenantUser){
            String id = tenantUser.getId();
            return BaseResponse.success(posterService.listPosterTemplate(Long.valueOf(id),posterType, posterName, false, pageNum, pageSize));
        }else {
            return BaseResponse.error(ExceptionCodeEnum.NOT_LOGIN);
        }
    }

    @GetMapping("detail/{id}")
    public BaseResponse<PosterTemplateVo> getDetail(@PathVariable Long id){
        return BaseResponse.success(posterService.getPosterTemplate(id));
    }

    @DeleteMapping("delete/{id}")
    public BaseResponse<Void> deleteTemplate(@PathVariable Long id){
        posterService.deletePosterTemplate(id);
        return BaseResponse.success();
    }


    @PostMapping("favorite")
    public BaseResponse<Long> createPosterFavorite(@RequestBody PosterFavoriteCreateVo posterFavoriteCreateVo) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof TenantUser tenantUser){
            String id = tenantUser.getId();
            return BaseResponse.success(posterService.createPosterFavorite(Long.valueOf(id), posterFavoriteCreateVo));
        }else {
            return BaseResponse.error(ExceptionCodeEnum.NOT_LOGIN);
        }
    }

    @PostMapping("unFavorite/{posterId}")
    public BaseResponse<Void> deletePosterFavorite(@PathVariable Long posterId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof TenantUser tenantUser){
            String id = tenantUser.getId();
            posterService.deletePosterFavorite(Long.valueOf(id), posterId);
            return BaseResponse.success();
        }else {
            return BaseResponse.error(ExceptionCodeEnum.NOT_LOGIN);
        }
    }

    @PostMapping("type/create")

    public BaseResponse<Long> createPosterType(@RequestBody PosterTypeCreateVo routeTypeCreateVo) {
        return BaseResponse.success(posterService.createPosterType(routeTypeCreateVo));
    }

    @GetMapping("type/list")
    public BaseResponse<BasePageRespVo<PosterTypeListVo>> listPosterType(PosterTypeQueryVo routeTypeQueryVo,
                                                                         @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                         @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return BaseResponse.success(posterService.listPosterType(routeTypeQueryVo, pageNum, pageSize));
    }

    @GetMapping("type/all")
    public BaseResponse<List<PosterTypeListVo>> listAllPosterType(PosterTypeQueryVo routeTypeQueryVo) {
        return BaseResponse.success(posterService.listAllPosterType());
    }

    @PutMapping("type/update/{id}")
    public BaseResponse<Boolean> updatePosterType(@PathVariable Long id, @RequestBody PosterTypeCreateVo routeTypeCreateVo) {
        posterService.updatePosterType(id, routeTypeCreateVo);
        return BaseResponse.success();
    }

//    public B

    @Autowired
    public void setPosterService(PosterService posterService) {
        this.posterService = posterService;
    }
}
